package com.apotik.legenda.dialog;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JDialog;

import com.apotik.legenda.dao.PatientsDao;
import com.apotik.legenda.model.Patients;
import com.apotik.legenda.model.SalesTrx;
import com.apotik.legenda.observer.PosObserver;
import com.apotik.legenda.util.CalendarUtil;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;

import java.awt.Font;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class InputPatientDialog extends JDialog implements ActionListener, KeyListener {
	private static final long serialVersionUID = 1L;

	private PosObserver observer;
	private SalesTrx st;
	private Patients patients;
	
	private JTextField textFieldAtasNama;
	private JTextField textFieldNamaPasien;
	private JTextField textFieldUmur;
	private JTextField textFieldHp;
	private JTextArea textAreaAlamat;
	private JButton btnSubmit;
	private JButton btnClose;
	
	
	public InputPatientDialog(PosObserver observer, SalesTrx st, Patients patient) {
		this.observer = observer;
		this.st = st;
		this.patients = patient;
		
		int widthFrame = 400;
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		
		String atasNama = "";
		String namaPasient = "";
		String umur = "";
		String nohp = "";
		String alamat = "";
		if(patient!=null){
			atasNama = patient.getResponsiblePerson();
			namaPasient = patient.getPatientName();
			umur = patient.getPatientAge()!=null ? patient.getPatientAge():"";
			nohp = patient.getPhoneNumber()!=null ? patient.getPhoneNumber():"";
			alamat = patient.getAddress()!=null ? patient.getAddress():"";
		}
		
		
		setTitle("Passien");
		setSize(widthFrame, 250);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		addKeyListener(this);
		setLocation((dim.width-widthFrame)/2, 100);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 3));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(0, 2, 0, 3));
		
		JLabel lblAtasNama = new JLabel("Atas Nama");
		lblAtasNama.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblAtasNama);
		
		textFieldAtasNama = new JTextField();
		textFieldAtasNama.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel_1.add(textFieldAtasNama);
		textFieldAtasNama.setColumns(10);
		textFieldAtasNama.setText(atasNama);
		
		JLabel lblNamaPasien = new JLabel("Nama Pasien");
		lblNamaPasien.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblNamaPasien);
		
		textFieldNamaPasien = new JTextField();
		textFieldNamaPasien.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel_1.add(textFieldNamaPasien);
		textFieldNamaPasien.setColumns(10);
		textFieldNamaPasien.setText(namaPasient);
		
		JLabel lblUmur = new JLabel("Umur");
		lblUmur.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblUmur);
		
		textFieldUmur = new JTextField();
		textFieldUmur.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel_1.add(textFieldUmur);
		textFieldUmur.setColumns(10);
		textFieldUmur.setText(umur);
		
		JLabel lblNomorHp = new JLabel("Nomor HP");
		lblNomorHp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblNomorHp);
		
		textFieldHp = new JTextField();
		textFieldHp.setFont(new Font("Tahoma", Font.PLAIN, 12));
		panel_1.add(textFieldHp);
		textFieldHp.setColumns(10);
		textFieldHp.setText(nohp);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(0, 2, 0, 3));
		
		JLabel lblAlamat = new JLabel("Alamat");
		lblAlamat.setVerticalAlignment(SwingConstants.TOP);
		panel_2.add(lblAlamat);
		lblAlamat.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		textAreaAlamat = new JTextArea();
		textAreaAlamat.setFont(new Font("Courier New", Font.PLAIN, 12));
		textAreaAlamat.setLineWrap(true);
		textAreaAlamat.setWrapStyleWord(true);
		textAreaAlamat.setText(alamat);
		
		JScrollPane scrollPane = new JScrollPane(textAreaAlamat);
		panel_2.add(scrollPane);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(200, 60));
		
		JPanel panel_3 = new JPanel();
		panel.add(panel_3, BorderLayout.SOUTH);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_3.add(panel_4, BorderLayout.EAST);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addKeyListener(this);
		btnSubmit.addActionListener(this);
		panel_4.add(btnSubmit);
		
		btnClose = new JButton("Close");
		btnClose.addKeyListener(this);
		btnClose.addActionListener(this);
		panel_4.add(btnClose);
		
	}
	
	private void btnSubmitAction(){
		String atasNama = textFieldAtasNama.getText().trim();
		String namaPasien = textFieldNamaPasien.getText().trim();
		String hp = textFieldHp.getText().trim();
		String umur = textFieldUmur.getText().trim();
		String alamat = textAreaAlamat.getText().trim();
		
		if(atasNama.equalsIgnoreCase("")){
			JOptionPane.showMessageDialog(null, "Atas Nama tidak boleh kosong", "Exception", JOptionPane.ERROR_MESSAGE);
			textFieldAtasNama.requestFocus();
			return;
		}
		
		if(namaPasien.equalsIgnoreCase("")){
			JOptionPane.showMessageDialog(null, "Nama Pasien tidak boleh kosong", "Exception", JOptionPane.ERROR_MESSAGE);
			textFieldNamaPasien.requestFocus();
			return;
		}
		
		Date dateNow = CalendarUtil.dateNow();
		
		Patients obj = null;
		if(this.patients!=null){
			obj = this.patients;
			obj.setUpdateDate(dateNow);
		}else{
			obj = new Patients();
			obj.setCreatedDate(dateNow);
			obj.setSalesTrx(st);
		}
		obj.setResponsiblePerson(atasNama);
		obj.setPatientName(namaPasien);
		obj.setPhoneNumber(hp);
		obj.setPatientAge(umur);
		obj.setAddress(alamat);
		
		PatientsDao dao = new PatientsDao();
		if(dao.saveOrUpdate(obj)){
			Criterion cr1 = Restrictions.eq("responsiblePerson", atasNama);
			Criterion cr2 = Restrictions.eq("patientName", namaPasien);
			Criterion cr3 = Restrictions.eq("salesTrx", st);
			List<Patients> listPatient = dao.loadBy(Order.asc("patientId"), cr1, cr2, cr3);
			Patients p = null;
			if(listPatient.size()>0){
				p = listPatient.get(0);	
			}
			
			HashMap<String, Object> hm = new HashMap<>();
			hm.put("_st", st);
			hm.put("patient", p);
			observer.insertCustomer(hm);
			this.setVisible(false);
		}
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		
		if(source==btnSubmit){
			btnSubmitAction();
		}else if(source==btnClose){
			this.setVisible(false);
		}
	}
	
	@Override
	public void keyPressed(KeyEvent ke) {
		if(ke.getKeyCode()==KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		
		Object source = ke.getSource();
		if(source==btnSubmit && ke.getKeyCode()==KeyEvent.VK_ENTER){
			btnSubmitAction();
		}else if(source==btnClose&& ke.getKeyCode()==KeyEvent.VK_ENTER){
			this.setVisible(false);
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
